package com.hare.harelearn.DataStructure.AlgorithmExercises;

/**
 * @program: HareLearn
 * @description:  学习二分查找的思想
 * @author: Hare
 * @create: 2025−02-24 17:51
 * @Version 1.0
 **/
public class 二分查找 {

    public static void main(String[] args) {
        int i = binarySearch(new int[]{5, 6, 7, 9, 10, 14, 15, 56, 90}, 56);
        System.out.println(i);
    }



    public static int binarySearch(int[] nums, int target) {
            int left = 0;
            int right = nums.length - 1;
            while (left <= right) {
                int mid = (left + right) >>> 1;
                if(target == nums[mid]){
                    return mid;
                }
                if(nums[mid] > target){
                    right = mid  - 1;
                }
                if(nums[mid] < target){
                    left = mid + 1;
                }
            }
            return -1;
    }
}
